<?php

include '../../../configuration.php';

$smarty->template_dir = LOCAL_PATH.'administrator/main/templates/';
$smarty->compile_dir = LOCAL_PATH.'administrator/main/templates_c/';
$smarty->config_dir = LOCAL_PATH.'administrator/main/configs';

$smarty->compile_check = true;
//$smarty->debugging = true;

if (isset($_POST['name'])){
	$name = $_POST['name'];
	$surname = $_POST['surname'];
	$group = $_POST['select_group'];
	$id = $_POST['id'];
	
//	echo "UPDATE `users` SET u_name = '$name', u_surname = '$surname', group_id = $group,  u_password = '$password' WHERE id = $id";
	if(isset($_POST['ch_pass'])) {
	$upassword = md5($_POST['new_password']);
	mysql_query("UPDATE `users` SET u_name = '$name', u_surname = '$surname', group_id = $group,  u_password = '$password' WHERE id = $id");
	}
	else{
		mysql_query("UPDATE `users` SET u_name = '$name', u_surname = '$surname', group_id = $group WHERE id = $id");
	}
	header("Location: modify_or_delete_user.php");
}
else if(isset($_GET['id'])){
	$id = $_GET['id'];
}else{
	header("Location: modify_or_delete_user.php");
}

$result = mysql_query("SELECT * FROM `users` as u, `groups` as g, `faculties` as f WHERE u.group_id = g.group_id AND g.faculty_id = f.faculty_id AND id = $id");
$row = mysql_fetch_assoc($result);
$name = $row["u_name"];
$surname = $row["u_surname"];
$group_id = $row["group_id"];
$group = $row["group_name"];
$login = $row["u_login"];
$email = $row["u_email"];
$faculty = $row["faculty_name"];
$f_id = $row["faculty_id"];

$groups = array();
$group_ids = array();
$result2 = mysql_query("SELECT * FROM `groups` WHERE faculty_id = $f_id");
while($rows2 = mysql_fetch_array($result2,MYSQL_ASSOC)){
	$groups[] = $rows2["group_name"];
	$group_ids[] = $rows2["group_id"];
}

$smarty->assign('faculty',$faculty);
$smarty->assign('name',$name);
$smarty->assign('surname',$surname);
$smarty->assign('id',$id);
$smarty->assign('group',$group);
$smarty->assign('group_id',$group_id);
$smarty->assign('login',$login);
$smarty->assign('email',$email);
$smarty->assign('groups',$groups);
$smarty->assign('group_ids',$group_ids);


include '../../check.php';
if ($checking == 1)
	$smarty->display('modifing_user.tpl');
else echo "You are not autorised"; exit;

?>